package com.ksu.manager.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.ksu.manager.model.Product;
import com.ksu.manager.model.TypeItem;

import com.ksu.manager.service.IProductService;
import com.ksu.manager.service.ITypeItemService;


/**
 * 库存管理controller
 * 
 * @author lambert
 * 
 */

@Controller
@RequestMapping("/product")
public class ProductController {

	@SuppressWarnings("unused")
	private static Logger log = Logger.getLogger(ProductController.class);
	private IProductService productService;
	private ITypeItemService typeItemService;




	public ITypeItemService getTypeItemService() {
		return typeItemService;
	}

	public void setTypeItemService(ITypeItemService typeItemService) {
		this.typeItemService = typeItemService;
	}

	public IProductService getProductService() {
		return productService;
	}

	public void setProductService(IProductService productService) {
		this.productService = productService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response,Product product) {
		ModelAndView mv = new ModelAndView("/product/list");
		try {
			if(product.getName()!=null && !product.getName().equals("")){
			    product.setName(new String(product.getName().getBytes("ISO-8859-1"),"UTF-8"));
			}
			List<Product> productList = productService.queryList(product);
			List<TypeItem> typeItemList = typeItemService.queryList(new TypeItem());
			mv.addObject("productList", productList);
			mv.addObject("typeItemList", typeItemList);
			
		} catch (Exception e) {
			e.printStackTrace();
		}

		return mv;
	}
	
	@RequestMapping(value = "/add")
	public ModelAndView add(HttpServletRequest request, HttpServletResponse response,Product product){
		if(product!=null){
			productService.add(product);
		}
		product.setName(null);
		return query(request,response,product);
	}
	
	@RequestMapping(value = "/del")
	public ModelAndView del(HttpServletRequest request, HttpServletResponse response,Product product){
		if(product!=null){
			productService.del(product);
		}
		return query(request,response,product);
	}
	
	@RequestMapping(value = "/mod")
	public ModelAndView mod(HttpServletRequest request, HttpServletResponse response,Product product){
		if(product!=null){
			productService.mod(product);
		}
		ModelAndView mv = new ModelAndView("/login/result");
		mv.addObject("result","refreshParent");
		return mv;
	}
	
	@RequestMapping(value = "/getProduct")
	public ModelAndView getTypeItemById(HttpServletRequest request, HttpServletResponse response,Product product){
		ModelAndView mv = new ModelAndView("/product/mod");
		product = productService.queryProductById(product);
		List<TypeItem> typeItemList = typeItemService.queryList(new TypeItem());
		mv.addObject(product);
		mv.addObject(typeItemList);
		return mv; 
	}

}
